<script setup lang="ts">
/**
 * 登录注册页面
 * @author 高桥凉介
 * @date 2024-12-23 18:10:13
 */
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import type {LoginForm, Res} from "@/types";
import {userLogin} from "@/api/user.ts";
import router from "@/router";
import type {ApiResponse} from "@/utils/httpRequest.ts";


// 登录表单的数据和校验规则
const loginForm: LoginForm = reactive({
  userAccount: "",
  password: "",
});

const rules = {
  userAccount: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 20, message: "用户名长度在 3 到 20 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度在 6 到 20 个字符", trigger: "blur" },
  ],
};

// 登录按钮状态
const isLoggingIn = ref(false);

// 模拟登录函数
const handleLogin = async () => {
  if (!loginForm.userAccount || !loginForm.password) {
    ElMessage.error("请输入完整的用户名和密码！");
    return;
  }

  // 设置登录中状态
  isLoggingIn.value = true;
  userLogin(loginForm).then(res =>{
    if (res.code === 2000 && res.data){
        ElMessage.success('登录成功')
        router.push('/')
    }
  })
  isLoggingIn.value = false;
};
</script>

<template>
  <div class="login-page">
    <el-card shadow="always" class="card">
      <h2 class="login-title">登录</h2>
      <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
          label-width="0"
          class="login-form"
      >
        <el-form-item prop="username">
          <div class="input-wrapper">
            <span class="input-label">账号：</span>
            <el-input
                v-model="loginForm.userAccount"
                placeholder="请输入用户名"
                prefix-icon="el-icon-user"
                clearable
            />
          </div>
        </el-form-item>

        <el-form-item prop="password">
          <div class="input-wrapper">
            <span class="input-label">密码：</span>
            <el-input
                v-model="loginForm.password"
                placeholder="请输入密码"
                prefix-icon="el-icon-lock"
                show-password
                clearable
            />
          </div>
        </el-form-item>

        <el-button
            type="primary"
            class="login-button"
            :disabled="isLoggingIn"
            @click="handleLogin"
        >
          {{ isLoggingIn ? "登录中..." : "登录" }}
        </el-button>

        <div class="form-footer">
          <a href="#" class="forgot-password">忘记密码？</a>
          <a href="/register" class="register-link">注册账号</a>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
/* 页面整体样式 */
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url("../assets/bbgg.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 1rem;
  box-sizing: border-box;
}

.card {
  width: 400px;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 15px;
  background-color: rgba(255, 255, 255, 0.8);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  font-size: 2rem;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 2rem;
}

/* 表单样式 */
.login-form {
  width: 100%;
}

.el-form-item {
  margin-bottom: 1.2rem;
}

.input-wrapper {
  display: flex;
  align-items: center;
  width: 100%;
}

.input-label {
  font-size: 1rem;
  color: #333;
  width: 60px;
  margin-right: 10px;
  line-height: 32px; /* 确保标签与输入框垂直居中 */
}

.el-input {
  flex-grow: 1;
  border-radius: 8px;
  padding: 0.5rem 1rem;
  font-size: 1rem;
}

/* 登录按钮样式 */
.el-button.login-button {
  display: block;
  width: 100%;
  max-width: 300px;
  margin: 1.5rem auto;
  padding: 0.8rem;
  font-size: 1.2rem;
  font-weight: bold;
  text-align: center;
  color: #ffffff;
  background-color: #007bff;
  border: 2px solid #007bff;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.el-button.login-button:hover {
  background-color: #0056b3;
  border-color: #0056b3;
}

.el-button.login-button:active {
  background-color: #003d82;
  border-color: #003d82;
  transform: scale(0.98);
}

.el-button.login-button:disabled {
  background-color: #d6d6d6;
  border-color: #d6d6d6;
  color: #ffffff;
  cursor: not-allowed;
  opacity: 0.6;
}

.form-footer {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  color: #636e72;
  margin-top: 1rem;
}

.forgot-password:hover,
.register-link:hover {
  color: #0984e3;
  text-decoration: underline;
}

/* 输入框和按钮的响应式优化 */
@media (max-width: 500px) {
  .card {
    width: 100%;
    padding: 1.5rem;
  }

  .login-title {
    font-size: 1.5rem;
  }

  .el-input,
  .el-button {
    font-size: 0.9rem;
  }

  .form-footer {
    flex-direction: column;
    align-items: center;
  }

  .form-footer a {
    margin: 0.5rem 0;
  }
}
</style>
